﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace VSM.UC
{
    public partial class UCSOPending : System.Web.UI.UserControl
    {
        private string TargetPageUrl = "/Business/Sales/SOPendingEdit.aspx";

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    if (Session[VSM.Utils.AppConstants.U_Department].ToString() == VSM.Utils.AppConstants.SALES)
                    {
                        if (Session[VSM.Utils.AppConstants.ID_Dealer].ToString() == VSM.Utils.AppConstants.VSM)
                        {
                            string dbType = (string)Session[Utils.AppConstants.ID_Dealer] == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
                            LoadData(dbType);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string alert = "alert('Error:" + ex.Message.Replace('\'', ' ') + " ');";
                ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", alert, true);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvSearchResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            DataTable tbUCSOPending = (DataTable)Session["UCSOPending"];
            this.grvItem.PageIndex = e.NewPageIndex;
            BindItem(tbUCSOPending);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvSearchResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");
                e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");
                e.Row.Attributes["style"] = "cursor:pointer";
            }
        }

        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkDocEntry_Click(object sender, EventArgs e)
        {
            try
            {
                GridViewRow gr = (GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent;
                LinkButton lnkDocEntry = (LinkButton)gr.FindControl("lnkDocEntry");
                Response.Redirect(TargetPageUrl + "?order_id=" + lnkDocEntry.Text);
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void LoadData(string dbType)
        {
            MasterData masterData = new MasterData();
            DataSet dsSOPendingList = masterData.GetMaketingDocPendingList(dbType, Session[Utils.AppConstants.ID_Dealer].ToString(), ConvertDocType.Get2String(DocType.SALES_ORDER), Utils.AppConstants.SALES);
            BindItem(dsSOPendingList.Tables[0]);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tb"></param>
        private void BindItem(DataTable tb)
        {
            Session["UCSOPending"] = tb;
            this.grvItem.DataSource = tb;
            this.grvItem.DataBind();
        }

    }
}